package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 一.管理端：1.1员工管理控制器
 * 提供员工登录、退出、等功能接口
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService; // 员工服务接口，用于处理员工相关的业务逻辑
    @Autowired
    private JwtProperties jwtProperties; // JWT属性配置，包含密钥和过期时间等信息

    /**
     * 1.员工登录接口
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO)   {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //*：登录成功后，生成jwt令牌(这个是特殊的，只有登录接口需要有这个，其它都没有)
        // 创建一个用于存储JWT声明（claims）的Map集合
        Map<String, Object> claims = new HashMap<>();
        // 向claims中添加员工ID信息，键为JwtClaimsConstant.EMP_ID，值为员工ID
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        // 使用JWT工具类生成token，传入密钥、过期时间和claims
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),  // 获取管理员密钥
                jwtProperties.getAdminTtl(),        // 获取管理员token过期时间
                claims);                           // 传入claims信息

        // 构建员工登录返回对象(VO)
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())             // 设置员工ID
                .userName(employee.getUsername())  // 设置用户名
                .name(employee.getName())          // 设置姓名
                .token(token)                      // 设置生成的token
                .build();                          // 构建对象

        return Result.success(employeeLoginVO);
    }

    /**
     * 2.退出登录接口
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 3.新增员工接口
     */
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工成功:{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }


    /**
     * 4.分页查询员工接口
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询,参数为:{}",employeePageQueryDTO);
        PageResult pageResult =employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 5.启用或禁用员工接口
     * @param status
     * @param id
     */
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用（1）或禁用（0）员工:{},账号:{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 6.根据id查询员工接口
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工:{}",id);
        Employee employee =employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 7.编辑员工信息
     * @param employeeDTO
     */
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}